package com.ruoyi.wm.controller;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.wm.domain.WmMedicines;
import com.ruoyi.wm.service.IWmMedicinesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wm.domain.WmMedicineInstorageList;
import com.ruoyi.wm.service.IWmMedicineInstorageListService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.jws.WebParam;

/**
 * 药品验收入库记录Controller
 * 
 * @author hpf
 * @date 2021-11-25
 */
@Controller
@RequestMapping("/wm/medicine_instorage")
public class WmMedicineInstorageListController extends BaseController
{
    private String prefix = "wm/medicine_instorage";

    @Autowired
    private IWmMedicineInstorageListService wmMedicineInstorageListService;
    @Autowired
    private IWmMedicinesService wmMedicinesService;

    @RequiresPermissions("wm:medicine_instorage:view")
    @GetMapping()
    public String medicine_instorage()
    {
        return prefix + "/medicine_instorage";
    }

    /**
     * 查询药品验收入库记录列表
     */
    @RequiresPermissions("wm:medicine_instorage:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmMedicineInstorageList wmMedicineInstorageList)
    {
        startPage();
        List<WmMedicineInstorageList> list = wmMedicineInstorageListService.selectWmMedicineInstorageListList(wmMedicineInstorageList);
        return getDataTable(list);
    }

    /**
     * 导出药品验收入库记录列表
     */
    @RequiresPermissions("wm:medicine_instorage:export")
    @Log(title = "药品验收入库记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmMedicineInstorageList wmMedicineInstorageList)
    {
        List<WmMedicineInstorageList> list = wmMedicineInstorageListService.selectWmMedicineInstorageListList(wmMedicineInstorageList);
        ExcelUtil<WmMedicineInstorageList> util = new ExcelUtil<WmMedicineInstorageList>(WmMedicineInstorageList.class);
        return util.exportExcel(list, "medicine_instorage");
    }

    /**
     * 新增药品验收入库记录
     */
    @GetMapping("/add")
    public String add(WmMedicines wmMedicines,Model model)
    {
        Collection<WmMedicines> list = wmMedicinesService.selectWmMedicinesList(wmMedicines);
        model.addAttribute("medicines",list);
        return prefix + "/add";
    }

    /**
     * 新增保存药品验收入库记录
     */
    @RequiresPermissions("wm:medicine_instorage:add")
    @Log(title = "药品验收入库记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmMedicineInstorageList wmMedicineInstorageList)
    {
        AjaxResult result = toAjax(wmMedicineInstorageListService.insertWmMedicineInstorageList(wmMedicineInstorageList));
        int n =  wmMedicineInstorageListService.updateMedicineStock();
        return result;
    }

    /**
     * 修改药品验收入库记录
     */
    @GetMapping("/edit/{ID}")
    public String edit(@PathVariable("ID") Long ID, ModelMap mmap)
    {
        WmMedicineInstorageList wmMedicineInstorageList = wmMedicineInstorageListService.selectWmMedicineInstorageListById(ID);
        mmap.put("wmMedicineInstorageList", wmMedicineInstorageList);
        return prefix + "/edit";
    }

    /**
     * 修改保存药品验收入库记录
     */
    @RequiresPermissions("wm:medicine_instorage:edit")
    @Log(title = "药品验收入库记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmMedicineInstorageList wmMedicineInstorageList)
    {
        AjaxResult result = toAjax(wmMedicineInstorageListService.updateWmMedicineInstorageList(wmMedicineInstorageList));
        int n =  wmMedicineInstorageListService.updateMedicineStock();
        return result;
    }

    /**
     * 删除药品验收入库记录
     */
    @RequiresPermissions("wm:medicine_instorage:remove")
    @Log(title = "药品验收入库记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        AjaxResult result=toAjax(wmMedicineInstorageListService.deleteWmMedicineInstorageListByIds(ids));
        int n =  wmMedicineInstorageListService.updateMedicineStock();
        return result;
    }
}
